package com.atguigu.zhxy.service.impl;

import com.atguigu.zhxy.mapper.AdminMapper;
import com.atguigu.zhxy.pojo.Admin;
import com.atguigu.zhxy.pojo.LoginForm;
import com.atguigu.zhxy.service.AdminService;
import com.atguigu.zhxy.util.MD5;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service("adminServiceImpl")
@Transactional
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
    @Override
    public Admin login(LoginForm loginForm) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>() ;
        queryWrapper.eq("name",loginForm.getUsername()) ;
        queryWrapper.eq("password", MD5.encrypt(loginForm.getPassword())) ;
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public Admin getAdminById(Long userId) {
        Admin admin = baseMapper.selectById(userId);
        return admin;
    }

    @Override
    public IPage<Admin> getAdminByPN(Page<Admin> page, String name) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        Page<Admin> adminPage = baseMapper.selectPage(page, queryWrapper);
        return adminPage;
    }
//
//    @Override
//    public void getAdmin(String name,String password) {
//        if(!StringUtils.isEmpty(password)){
//            QueryWrapper<Admin> queryWrapper = new QueryWrapper<>() ;
//            queryWrapper.eq("name",name) ;
//            Admin admin  = baseMapper.selectOne(queryWrapper);
//            String encrypt = MD5.encrypt(password);
//            admin.setPassword(encrypt);
//            this.baseMapper.updateById(admin) ;
//        }
//    }
}
